﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using BusinessLogic.Interfaces;
using Domain;
using Domain.Entities;

namespace BusinessLogic.Implementations
{
    public class EFCategoryRepository:ICategoryRepository
    {
        private EFDbContext context;

        public EFCategoryRepository(EFDbContext context)
        {
            this.context = context;
        }


        public IEnumerable<Category> GetCategories()
        {
            return context.Categories.ToList();
        }

        public Category GetCategoryById(int id)
        {
            return context.Categories.FirstOrDefault(i => i.Id == id);
        }

        public Category GetCategoryByEnglishName(string name)
        {
           return context.Categories.FirstOrDefault(i => i.EnglishName  == name);
        }

       

        public void CreateCategory(Category category)
        {
            context.Categories.Add(category);
        }

       
        public void SaveCategory(Category category)
        {
            if (category.Id == 0)
            {
                context.Categories.Add(category);
            }
            else
            {
                context.Entry(category).State = EntityState.Modified;
            }
            context.SaveChanges();
        }

        public void DeleteCategoryById(int id)
        {
            context.Categories.Remove(GetCategoryById(id));
            context.SaveChanges();
        }
    }
}
